<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\finance\model\Classofreim as ClassofreimModel;
use app\shop\model\Shop as ShopModel;
use app\system\model\systemUser as systemUserModel;
use app\shouyin\model\Reim as ReimModel;
use app\finance\model\Account as AccountModel;
use think\Db;
class Reim extends Admin
{
    protected $hisiModel = 'Reim';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
    protected function initialize()
    {
        parent::initialize();
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $category = ClassofreimModel::getSelect(ClassofreimModel::getChilds());
            $this->assign('category', $category);
            $shops = ShopModel::getSelect(ShopModel::all());
            $this->assign('shops', $shops);
            $admins = systemUserModel::getSelect(systemUserModel::all());
            $this->assign('admins', $admins);
            $accounts = AccountModel::getSelect(AccountModel::all());
            $this->assign('accounts', $accounts);
        }
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $data['data'] = ReimModel::where($where)
                            ->field('*,class_id as class_name, shop_id as shop_name,admin_id as admin_name, finance_id as finance_name,account_id as account_name')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $data['count'] = ReimModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);
        }
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {// 数据验证
            $postData = $this->request->post();
        }
        $where = [];
        $db = Db::name('reim');
        $pk = $db->getPk();
        $id = $this->request->param($pk);

        $where[]= [$pk, '=', $id];
        $where  = $this->getRightWhere($where);
        Db::startTrans();
        if ($this->request->isPost()) {
            $postData['usetime'] = strtotime($postData['usetime']);

            try {
                if (!$db->where($where)->update($postData)) {
                    throw new \Exception("保存失败");
                }

                if($postData['status'] == 2){
                    if($postData['account_id'] == 0){
                        throw new \Exception("请选择正确的支出账户");
                    }
                    $ins = [
                        'sn' => $postData['sn'],
                        'type' => 4,
                        'account_id' => $postData['account_id'],
                        'price' => $postData['price'],
                        'createtime' => time(),
                        'updatetime' => time(),
                        'is_effective' => 1,
                        'cate' => 2,
                    ];
                    Db::name('system_balance_log')->insert($ins);
                }
                // 提交事务
                Db::commit();
                // throw new \Exception("用户余额不足！");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return $this->error($e->getMessage(), false);
            }
            return $this->success('审核完成');
        }

        $formData = $db->where($where)->find();


        if (!$formData) {
            return $this->error('数据不存在或没有权限');
        }

        $this->assign('formData', $formData);

        $template = $this->request->param('template', 'form');

        return $this->fetch($template);
    }

}
